/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Administration.java
 *
 * Created on Dec 9, 2009, 6:05:11 PM
 */

package GUI;

import Business.Supplier_BS;
import Business.Supplier_MD;
import DAO.Supplier_DAO;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author anhvtn_b00545
 */
public class Supplier extends javax.swing.JPanel {

    /** Creates new form Administration */
    public Supplier() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblTypeSupplier = new javax.swing.JLabel();
        cbbTypeSupplier = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        btSearch = new javax.swing.JButton();
        txtNameSupplier = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbSupplier = new javax.swing.JTable();
        btDeleteSupplier = new javax.swing.JButton();
        btACreateSupplier = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Filter"));

        lblTypeSupplier.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblTypeSupplier.setText("Type: ");

        cbbTypeSupplier.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "Distributors", "Dealers", "Who Sale Retailers" }));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Name: ");

        btSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Filter.png"))); // NOI18N
        btSearch.setText("Search");
        btSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTypeSupplier)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbbTypeSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNameSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(btSearch)
                .addGap(151, 151, 151))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbTypeSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtNameSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTypeSupplier)
                    .addComponent(btSearch))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Supplier List"));

        tbSupplier.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Code", "Name", "Phone", "Address"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbSupplier.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbSupplierMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbSupplier);

        btDeleteSupplier.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Delete-icon.png"))); // NOI18N
        btDeleteSupplier.setText("Delete");
        btDeleteSupplier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteSupplierActionPerformed(evt);
            }
        });

        btACreateSupplier.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Add.png"))); // NOI18N
        btACreateSupplier.setText("Create");
        btACreateSupplier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btACreateSupplierActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(btACreateSupplier)
                        .addGap(18, 18, 18)
                        .addComponent(btDeleteSupplier)
                        .addGap(77, 77, 77))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 583, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btACreateSupplier)
                    .addComponent(btDeleteSupplier))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btACreateSupplierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btACreateSupplierActionPerformed
        // TODO add your handling code here:
        CreateSupplier supplier = new CreateSupplier(null, true);
        supplier.setVisible(true);
        
    }//GEN-LAST:event_btACreateSupplierActionPerformed

    private void btSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed
        // TODO add your handling code here:
        List<Supplier_BS> suppliers = Supplier_DAO.getSuppliers(cbbTypeSupplier.getSelectedIndex() - 1,
                txtNameSupplier.getText());
        tbSupplier.setModel(new Supplier_MD(suppliers));
    }//GEN-LAST:event_btSearchActionPerformed

    private void btDeleteSupplierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteSupplierActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this Supplier?")
                == JOptionPane.YES_OPTION) {
            int row = tbSupplier.getSelectedRow();
            Supplier_BS supplier = getSupplierAtRow(row);
            Supplier_DAO.delete(supplier.getsID());
            ((Supplier_MD) tbSupplier.getModel()).deleteSupplier(supplier);
        }
    }//GEN-LAST:event_btDeleteSupplierActionPerformed

    private void tbSupplierMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbSupplierMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() > 1) {
            int row = tbSupplier.getSelectedRow();
            Supplier_BS selectedSupplier = getSupplierAtRow(row);

            SupplierDetails SD = new SupplierDetails(null, true);
            SD.setSupplier(selectedSupplier);
            SD.setVisible(true);

            ((Supplier_MD) tbSupplier.getModel()).updateSupplier(selectedSupplier);
        }
    }//GEN-LAST:event_tbSupplierMouseClicked

    private Supplier_BS getSupplierAtRow(int rowIndex) {
        Supplier_MD model = (Supplier_MD) tbSupplier.getModel();
        return model.getSupplier(rowIndex);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btACreateSupplier;
    private javax.swing.JButton btDeleteSupplier;
    private javax.swing.JButton btSearch;
    private javax.swing.JComboBox cbbTypeSupplier;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblTypeSupplier;
    private javax.swing.JTable tbSupplier;
    private javax.swing.JTextField txtNameSupplier;
    // End of variables declaration//GEN-END:variables

}
